<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateSlideshowRequest;
use App\Http\Requests\UpdateSlideshowRequest;
use App\Repositories\PicturesRepository;
use App\Repositories\SlideshowRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class SlideshowController extends AppBaseController
{
    /** @var  SlideshowRepository */
    private $slideshowRepository;
    private $pic;

    public function __construct(SlideshowRepository $slideshowRepo, PicturesRepository $picturesRepository)
    {
        $this->slideshowRepository = $slideshowRepo;
        $this->pic = $picturesRepository;
    }

    /**
     * Display a listing of the Slideshow.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->slideshowRepository->pushCriteria(new RequestCriteria($request));
        $slideshows = $this->slideshowRepository->all();

        return view('slideshows.index')
            ->with('slideshows', $slideshows);
    }

    /**
     * Show the form for creating a new Slideshow.
     *
     * @return Response
     */
    public function create()
    {
        return view('slideshows.create');
    }

    /**
     * Store a newly created Slideshow in storage.
     *
     * @param CreateSlideshowRequest $request
     *
     * @return Response
     */
    public function store(CreateSlideshowRequest $request)
    {
        $input = $request->all();

        if($request->file("image")){
            $image = $this->pic->uploadByHash($request->file('image'),"slideshow",null,null);
            $input['image'] = $image->url;
        }

        $slideshow = $this->slideshowRepository->create($input);

        Flash::success('Slideshow saved successfully.');

        return redirect(route('slideshows.index'));
    }

    /**
     * Display the specified Slideshow.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $slideshow = $this->slideshowRepository->findWithoutFail($id);

        if (empty($slideshow)) {
            Flash::error('Slideshow not found');

            return redirect(route('slideshows.index'));
        }

        return view('slideshows.show')->with('slideshow', $slideshow);
    }

    /**
     * Show the form for editing the specified Slideshow.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $slideshow = $this->slideshowRepository->findWithoutFail($id);

        if (empty($slideshow)) {
            Flash::error('Slideshow not found');

            return redirect(route('slideshows.index'));
        }

        return view('slideshows.edit')->with('slideshow', $slideshow);
    }

    /**
     * Update the specified Slideshow in storage.
     *
     * @param  int              $id
     * @param UpdateSlideshowRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateSlideshowRequest $request)
    {
        $slideshow = $this->slideshowRepository->findWithoutFail($id);

        if (empty($slideshow)) {
            Flash::error('Slideshow not found');

            return redirect(route('slideshows.index'));
        }
        $input = $request->all();
        if($request->file("image")){
            $image = $this->pic->uploadByHash($request->file('image'),"slideshow",null,null);
            $input['image'] = $image->url;
        }
        $slideshow = $this->slideshowRepository->update($input, $id);

        Flash::success('Slideshow updated successfully.');

        return redirect(route('slideshows.index'));
    }

    /**
     * Remove the specified Slideshow from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $slideshow = $this->slideshowRepository->findWithoutFail($id);

        if (empty($slideshow)) {
            Flash::error('Slideshow not found');

            return redirect(route('slideshows.index'));
        }

        $this->slideshowRepository->delete($id);

        Flash::success('Slideshow deleted successfully.');

        return redirect(route('slideshows.index'));
    }
}
